<?php
namespace App\Repositories\Presenter;

class PermissionRolePresenter
{
	public function getPermissionTree($permission,$permissionByRole){
		$result = "";
		$result = $this->treeTable($permission,$permissionByRole);
//		dd($result);
		return $result;
	}
	
	public function treeTable($permission,$permissionByRole,$pid = 0){
		$item = "";
		$currentLevel = $permission->where('menupid','=',$pid);
		foreach($currentLevel as $v){
			$item .= '<tr id="'.$v->menuid.'"';
			if($pid != 0){
				$item .=' pid="'.$pid.'"';
			}
			$item .= '>';
			$item .= '<td><input type="checkbox" name="menuid[]" ';
			if($permissionByRole->where('permission_id','=',$v->id)->first()){
				$item .= 'checked = "true"';
			}
			$item .= ' value="'.$v->id.'" onclick="toggleCheck(this)">'.$v->display_name.'</td></tr>';
			$item .= $this->treeTable($permission,$permissionByRole,$v->menuid);
		}
		return $item;
	}
	
	private function isSelected($permissionByRole,$permission){
		
	}
}